<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use DB;
use DateTimeInterface;

class Miniappsorder extends Model
{
    protected $table = 'miniapps_order';
    protected $primaryKey = 'id';

    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->format($this->dateFormat ?: 'Y-m-d H:i:s');
    }

    public static function addMiniappOrder($users_id,$order_id,$name,$corp_type,$service_type,$price)
    {
        return self::insert([
            'users_id'   => $users_id,
            'order_id'   => $order_id,
            'name'       => $name,
            'corp_type' => $corp_type,
            'service_type'   => $service_type,
            'price'   => $price,
            'created_at' => time(),
        ]);
    }

    public static function updatePayStatus($order_id,$data)
    {
        return self::where("order_id",$order_id)->update($data);
    }

    public static function userMiniappOrderList($users_id)
    {
        return self::where("users_id",$users_id)->where("pay_status",2)->get();
    }

    public static function lastMiniappOrder($users_id,$corp_type,$service_type)
    {
        return self::select("name","disable")->where("users_id",$users_id)->where("pay_status",2)->where("disable",1)->where("corp_type",$corp_type)->where("service_type",$service_type)->orderBy("id","DESC")->first();
    }

    public static function miniappOrderPayStatus($users_id,$order_id)
    {
        return self::select("pay_status")->where("users_id",$users_id)->where("order_id",$order_id)->orderBy("id","DESC")->first();
    }

    public static function orderPayStatus($order_id)
    {
        return self::select("pay_status","price")->where("order_id",$order_id)->orderBy("id","DESC")->first();
    }

    public static function miniappOrderExist($users_id,$corp_type)
    {
        return self::select("order_id")->where("users_id",$users_id)->where("corp_type",$corp_type)->where("pay_status",3)->where("disable",1)->orderBy("id","DESC")->first();
    }

    public static function miniappOrderDisable($id)
    {
        return self::where("id",$id)->update(["disable"=>2]);
    }
}
